package com.itheima02.day04.service;

import com.itheima02.day04.dao.StudentDao;
import com.itheima02.day04.domain.Student;
import com.itheima02.day04.domain.Teacher;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author 刘全
 * @date 2021/7/19
 */
public class StudentService {
    StudentDao dao = new StudentDao();

    public boolean OnLine(String s, String num) {
        return dao.OnLine(s, num);
    }

    public boolean addStudent(Student student) {
        return dao.addStudent(student);
    }

    public boolean isExist(String id) {
        return dao.isExist(id);
    }

    public ArrayList<Student> checkStudent() {
        return dao.checkStudent();
    }

    public boolean deleteStudent(String id) {
        ArrayList<Student> stuList = dao.deleteStudent();
        for (int i = 0; i < stuList.size(); i++) {
            if (id.equals(stuList.get(i).getId())) {
                stuList.remove(i);
                return true;
            }
        }
        return false;
    }

    public int getListSize() {
        return dao.getListSize();
    }

    public boolean setStudent(String id) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> stuList = dao.setStudent();
        for (int i = 0; i < stuList.size(); i++) {
            if (id.equals(stuList.get(i).getId())) {
                String newId;
                while (true) {
                    System.out.println("请输入新的学生学号:");
                    newId = sc.next();
                    boolean b = isExist(newId);
                    if (b) {
                        System.out.println("该学号已存在,请重新修改!");
                    } else {
                        break;
                    }
                }
                System.out.println("请输入新的学生姓名:");
                String newName = sc.next();
                System.out.println("请输入新的学生年龄:");
                int newAge = sc.nextInt();
                System.out.println("请输入新的学生生日:年-月-日");
                String newBirthday = sc.next();
                Student stu = new Student(newId, newName, newAge, newBirthday);
                stuList.set(i, stu);
                return true;
            }
        }
        return false;
    }

    public boolean addTeacher(Teacher teacher) {
        Teacher[] arr = dao.addTeacher();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == null) {
                arr[i] = teacher;
                return true;
            }
        }
        return false;
    }

    public Teacher[] checkTeacher() {
        return dao.checkTeacher();
    }

    public boolean deleteTeacher(String id) {
        Teacher[] arr = dao.deleteTeacher();
        for (int i = 0; i < arr.length; i++) {
            if ((arr[i] != null) && id.equals(arr[i].getId())) {
                arr[i] = null;
                return true;
            }
        }
        return false;
    }

    public Teacher[] getArr() {
        return dao.getArr();
    }

    public boolean setTeacher(String id) {
        Teacher[] arr = getArr();
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null && id.equals(arr[i].getId())) {
                System.out.println("请输入新的教师姓名:");
                String newName = sc.next();
                System.out.println("请输入新的教师年龄:");
                int newAge = sc.nextInt();
                System.out.println("请输入新的教师生日:");
                String newBirthday = sc.next();
                Teacher tea = new Teacher(id,newName, newAge, newBirthday);
                arr[i] = tea;
                return true;
            }
        }
        return false;
    }
}
